package com.wyz.saas.app

import android.app.Application
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.Log
import androidx.multidex.MultiDex
import com.atom.core.AtomApi
import com.tencent.mmkv.MMKV
import com.wyz.saas.api.factory.DispatcherManager
import com.wyz.saas.common.base.AbstractApiApplication
import com.wyz.saas.common.utils.Logs
import com.wyz.saas.module.chat.ChatModuleApplication
import com.wyz.saas.module.patrol.PatrolModuleApplication
import timber.log.Timber

class App : AbstractApiApplication() {

    companion object {
        fun getInstance(): App {
            return applicationInstance as App
        }
    }

    override fun initApplication(): Application {
        return this
    }

    override fun onCreate() {
        super.onCreate()
        // 65535方法框架
        MultiDex.install(this)

        Logs.init(this.applicationInfo.name)
        // msg框架
        checkMain {
            MMKV.initialize(this)
            Timber.plant(Timber.DebugTree())
            Timber.plant(CrashReportingTree())
            ChatModuleApplication(this)
            PatrolModuleApplication(this)
        }
    }

    override fun initHandle(): Handler {
        return object : Handler(Looper.getMainLooper()) {
            val RECEIVE_MESSAGE_DATA = 0 //接收到消息
            val RECEIVE_CLIENT_ID = 1 //cid通知
            val RECEIVE_ONLINE_STATE = 2 //cid在线状态通知消息
            val SHOW_TOAST = 3 //Toast消息
            override fun handleMessage(msg: Message) {
                when (msg.what) {
                    RECEIVE_MESSAGE_DATA -> {
                        onMessageArrived(msg.obj as String?)
                    }
                    RECEIVE_CLIENT_ID -> {
                        onClientInit(msg.obj as String?)
                    }
                    RECEIVE_ONLINE_STATE -> {
                        val online_state = msg.obj as String
                    }
                }
            }

            /**
             * 当Id初始化的试试
             */
            private fun onClientInit(cid: String?) {
                // 设置设备Id
                Account.setPushId(cid)
                if (Account.isLogin()) {
                    // 账户登录状态，进行一次PushId绑定
                    // 没有登录是不能绑定PushId的
                    //AccountHelper.bindPush(null)
                }
            }

            /**
             * 消息达到时
             */
            private fun onMessageArrived(message: String?) {
                Log.e("GetuiSdkDemo_App", "onMessageArrived $message")
                // 交给Factory处理
                message?.also {
                    AtomApi.getInstance().getImpl(DispatcherManager::class.java).dispatch(it)
                }
            }
        }
    }

    class CrashReportingTree : Timber.Tree() {
        override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
            if (priority == Log.VERBOSE || priority == Log.DEBUG) {
                return;
            }
            //FakeCrashLibrary.log(priority, tag, message);
            if (t != null) {
                if (priority == Log.ERROR) {
                    //FakeCrashLibrary.logError(t);
                } else if (priority == Log.WARN) {
                    // FakeCrashLibrary.logWarning(t);
                } else {

                }
            }
        }

    }

}